package org.example.goods.api;

import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.example.commons.ApiResponse;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ItemFeignClientFallbackFactory implements FallbackFactory<ItemFeignClient> {

    @Override
    public ItemFeignClient create(Throwable cause) {
        log.error("服务熔断", cause);
        // 直接抛错
        if (cause instanceof FeignException) {
            throw new RuntimeException(cause);
        }
        return new ItemFeignClient() {
            @Override
            public ApiResponse<ItemResponse> getItem(ItemRequest request) {
                log.info("类型{}", cause.getClass().getName());
                return ApiResponse.success(new ItemResponse(null, "服务熔断啦"));
            }
        };
    }

}
